<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
    <el-breadcrumb-item :to="{ path: '/' }">
      <i class="el-icon-s-promotion"/>后台管理</el-breadcrumb-item>
    <el-breadcrumb-item>属性模板管理</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>
  <div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="100" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="300"></el-table-column>
      <el-table-column prop="pinyin" label="拼音" width="300"></el-table-column>
      <el-table-column prop="keywords" label="关键词" ></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>
      <el-table-column width="150" label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="medium"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="medium"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改属性模板" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="ID" :label-width="formLabelWidth">
          <el-input v-model="editForm.id" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="拼音" :label-width="formLabelWidth">
          <el-input v-model="editForm.pinyin" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="关键词" :label-width="formLabelWidth">
          <el-input v-model="editForm.keywords" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" :label-width="formLabelWidth">
          <el-input v-model="editForm.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm()">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      editForm: {
        id: '',
        name: '',
        keywords:'',
        pinyin: '',
        sort: ''
      },
      formLabelWidth: '120px'
    }
  },
  methods:{
    submitEditForm(){
      let url = 'http://localhost:9080/attribute-template/'+this.editForm.id+'/update';
      let formData = this.qs.stringify(this.editForm);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then((response)=>{
        let responseBody = response.data;
        if(responseBody.state==20000){
          this.$message.success('修改成功！');
          this.dialogFormVisible= false;
          this.loadAlbumList();
        }else if(responseBody.state == 40400){
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible= false;
              this.loadAlbumList();
            }
          });
        }else {
          this.$message.error(responseBody.message);
          this.dialogFormVisible= false;
        }
      })
    },
    openEditDialog(album){
      let url = 'http://localhost:9080/attribute-template/'+album.id;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let responseBody = response.data;
        if(responseBody.state==20000){
          this.editForm = responseBody.data;
          this.dialogFormVisible=true;
        }else{
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAlbumList();
            }
          });
        }
      })
    },
    openDeleteConfirm(album){
      console.log('尝试打开id='+album.id+'的删除框');
      this.$confirm('此操作将永久删除'+album.name+'，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(album);

      }).catch(() => {
      });
    },
    handleDelete(album){
      console.log('即将执行删除');
      let url = 'http://localhost:9080/attribute-template/'+album.id+'/delete';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response)=>{
        let responseBody = response.data;
        if(responseBody.state==20000){
          this.$message.success('删除成功!');
          this.loadAlbumList();
        }else if(responseBody.state==40400){
          this.$alert(responseBody.stateMessage, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAlbumList();
            }
          });
        }else{
          this.$message.error(responseBody.stateMessage);
        }
      });
    },
    loadAlbumList(){
      let url = 'http://localhost:9080/attribute-template/select';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let responseBody = response.data;
        if(responseBody.state==20000){
          this.tableData = responseBody.data;
        }
      })
    },
  },
  mounted() {
    this.loadAlbumList();
  }
}
</script>